#import pprint 
import xml.dom.minidom
from xml.dom.minidom import Node
import sys

def addUserXml(username,password,profile):
    doc = xml.dom.minidom.parse(xml_path)
    for node in doc.getElementsByTagName("cardserv-proxy"):
        #'''
        for node2 in node.getElementsByTagName("user-manager"):
            for node3 in node2.getElementsByTagName("auth-config"):
                new_elem = doc.createElement("user")
                new_elem.setAttribute("name", username)
                new_elem.setAttribute("password", password)
                new_elem.setAttribute("profiles", profile)
                new_elem.setAttribute("max-connections", "1")
                node3.appendChild(new_elem)
                print "Adicionando user: %s" % (username)        
                '''
                for node4 in node3.childNodes:
                    if node4.nodeType != Node.TEXT_NODE:
                        print node4.getAttribute("name")
                        #title += node3.data
                '''
            #mapping[isbn] = title
        #'''
    return doc.toxml()

def delUserXml(username):
    doc = xml.dom.minidom.parse(xml_path)
    for node in doc.getElementsByTagName("cardserv-proxy"):
        for node2 in node.getElementsByTagName("user-manager"):
            for node3 in node2.getElementsByTagName("auth-config"):
                
                for node4 in node3.childNodes:
                    
                    if node4.nodeType != Node.TEXT_NODE:
                        if node4.getAttribute("name") == username:
                            print "Apagando user: %s" % (node4.getAttribute("name"))                          
                            node3.removeChild(node4)
                            
                                                
    return doc.toxml()

#xml_path = "proxy.xml"
xml_path = "/root/cardservproxy/config/proxy.xml"
xml_action = "add"
xml_name = "teste1"
xml_password = "test_pass"
xml_profile = "myprofile"

if not len(sys.argv) < 2:  
    for v in sys.argv:
        if len(v.split("="))>1:
            param = v.split("=")[0].strip()
            valor = v.split("=")[1].strip()
            try:
                if param.lower().find("action")>-1:
                    xml_action = "%s" % (valor)   
                elif param.lower().find("path")>-1:
                    xml_path = "%s" % (valor)               
                elif param.lower().find("name")>-1:
                    xml_name = "%s" % (valor)   
                elif param.lower().find("pass")>-1:
                    xml_password = "%s" % (valor)   
                elif param.lower().find("profile")>-1:
                    xml_profile = "%s" % (valor)   
                else:
                    print "Comando %s nao identificado!" % (param)
                #eval('%s=%s' % (param,valor))
                print "Param: %s" % (param)
                print "Valor: %s" % (valor)
            except:
                "Eval Error!"
else:
    print "try use: python %s param=value" % (sys.argv[0])
    print "Batch Job using default params!"
    #sys.exit(1)

##define a acao e executa! :D
if xml_action.lower().find("add")>-1:
    xml_str = str(addUserXml(xml_name,xml_password,xml_profile))
    fileHandle = open (xml_path, 'w' )
    fileHandle.write ("%s" % (xml_str))
    fileHandle.close() 
    #print xml_str
    
elif xml_action.lower().find("remove")>-1:
    xml_str = str(delUserXml(xml_name))
    fileHandle = open (xml_path, 'w' )
    fileHandle.write ("%s" % (xml_str))
    fileHandle.close()     
    #print xml_str
else:
    print "Nao entendi o comando!"
